package com.fangcun.service.api.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fangcun.bean.api.user.request.UserWithdrawalRequest;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.encryption.AESUtils;
import com.fangcun.entity.User;
import com.fangcun.entity.UserTransactionFlow;
import com.fangcun.entity.UserVirtualWallet;
import com.fangcun.entity.UserWithdrawCardNumber;
import com.fangcun.entity.UserWithdrawCashList;
import com.fangcun.mapper.UserMapper;
import com.fangcun.mapper.UserTransactionFlowMapper;
import com.fangcun.mapper.UserVirtualWalletMapper;
import com.fangcun.mapper.UserWithdrawCardNumberMapper;
import com.fangcun.mapper.UserWithdrawCashListMapper;
import com.fangcun.service.api.UserWithdrawaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserWithdrawaServiceImpl implements UserWithdrawaService {

	@Autowired
	private UserVirtualWalletMapper userVirtualWalletMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserWithdrawCardNumberMapper userWithdrawCardNumberMapper;
	@Autowired
	private UserWithdrawCashListMapper userWithdrawCashListMapper;
	@Autowired
	private UserTransactionFlowMapper userTransactionFlowMapper;

	@Override
	public void add(UserWithdrawCardNumber request) throws ProException {
		// 待做验证
		if (userWithdrawCardNumberMapper.insertSelective(request) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	/** 展示 微信 支付宝 之后银行卡 **/
	@Override
	public PageInfo<UserWithdrawCardNumber> list(PageBean<UserWithdrawCardNumber> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<UserWithdrawCardNumber> list = userWithdrawCardNumberMapper.listForApi(request.getParam().getUserId());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	// 提现
	@Override
	public void addWithdrawal(UserWithdrawalRequest request) throws ProException {
		// 判断某人用户是否冻结
		User user = userMapper.selectByPrimaryKey(request.getUserId());
		if (user.getStatus() == 2) {
			throw new ProValiDataException(ErrCode.USER_IS_EXCEPTION.getReturnCode(), "请联系客服！");
		}
		// 拿到某人的钱包
		UserVirtualWallet wallet = userVirtualWalletMapper.selectByUserId(request.getUserId());
		if (!wallet.getSign().equals(PublicMethod.getMd5(wallet.getUserId() + "" + wallet.getBanlanceMoney() + "" + wallet.getFrozenMoney(), 1, true))) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "钱包金额验证");
		}
		// 总金额 和 提现金额 比较 总比提 小 就报错
		if (wallet.getBanlanceMoney().compareTo(request.getWithdrawFee()) == -1) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "提现金额超出钱包余额");
		}
		// 提现方式-输入密码//密码判断
		try {
			if (!PublicMethod.getMd5(AESUtils.encrypt(request.getPaypwd(), "SHUYINGKEJI12345"), 1, true).equals(wallet.getPaypwd())) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "请输入正确密码");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 查询选择哪个提交方式
		UserWithdrawCardNumber number = userWithdrawCardNumberMapper.selectByPrimaryKey(request.getCardId());
		if (number == null) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		request.setName(number.getName());
		request.setPhone(number.getPhone());
		request.setBankName(number.getBankName());
		request.setWithdrawWay(number.getWithdrawWay());
		request.setReceivableAccount(number.getReceivableAccount());
		request.setAddress(number.getAddress());
		// 提交成功就1
		request.setWithdrawStatus(1);
		// 提现单号
		// request.setCodeNumber(PublicMethod.getOrderNumber(PublicConstant.WITHDRAWAL_TYPE_1, null));
		if (userWithdrawCashListMapper.insertSelective(request) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		// 提交申请后 修改账户 冻结 加提现金额。 余额减 提现金额
		// 余额 减去 提现
		wallet.setBanlanceMoney(wallet.getBanlanceMoney().subtract(request.getWithdrawFee()));
		// 冻结 加 提现
		wallet.setFrozenMoney(wallet.getFrozenMoney().add(request.getWithdrawFee()));
		// 重新加密
		wallet.setSign(PublicMethod.getMd5(wallet.getUserId() + "" + wallet.getBanlanceMoney() + "" + wallet.getFrozenMoney(), 1, true));
		if (userVirtualWalletMapper.updateByPrimaryKeySelective(wallet) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		// 获取刚添加的id
		UserWithdrawCashList cash = userWithdrawCashListMapper.selectByNewest();
		if (cash == null) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "最新记录获取失败");
		}
		// 流水信息 添加
		UserTransactionFlow flow = new UserTransactionFlow();
		flow.setUserId(request.getUserId());
		flow.setResType(10);
		flow.setResId(cash.getId());
		flow.setFlowType(2);
		flow.setPrice(request.getWithdrawFee());
		flow.setReason("提现记录");
		if (userTransactionFlowMapper.insertSelective(flow) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
}
